package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;

import com.sczmgk.hotline_pzh.DesJson.Des3;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.HttpFailCallback;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.db.impl.UserDaoImpl;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.User;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.RegistItemView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/8 0008.
 */
public class PhoneNumberActivity extends BaseActivity {

    public static final String PHONE = "phone";

    /*跳转的请求码*/
    private static final int REQUEST_CODE = 100;

    /*需要登录的密码*/
    private RegistItemView mLoginPwd;

    /*下一步的按钮*/
    private Button mBtnNext;
    private User userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_number);

        CommonUtils.addActivity(this);
        setHeader();

        initView();

        initData();

        setListener();
    }

    /**
     * 设置头
     */
    private void setHeader() {
        setTitle(R.string.personal_info_phone_number);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //登录密码
        mLoginPwd = (RegistItemView) findViewById(R.id.riv_change_phone_login_password);

        //下一步的按钮
        mBtnNext = (Button) findViewById(R.id.btn_change_phone_next);

        //设置输入类型
        mLoginPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

    }

    /**
     * 初始化数据
     */
    private void initData() {
        int userid = CacheUtils.getIntData(PhoneNumberActivity.this, Constants.LOGIN_UID, 0);
        userInfo = UserDaoImpl.getIntance(PhoneNumberActivity.this).findByQuery(userid);
    }

    /**
     * 设置监听
     */
    private void setListener() {

        //下一步的点击事件
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String inputPwd = mLoginPwd.getText();
                if (Tools.isEmpty(inputPwd)) {
                    MyToast.show(PhoneNumberActivity.this, "请输入密码");
                    return;
                }

                loadingDialog = DialogFactory.getLoadingDialog(PhoneNumberActivity.this);
                DialogFactory.showDialog(loadingDialog);
                Map<String,Object> maps = new HashMap<String, Object>();
                maps.put("userid",CacheUtils.getIntData(PhoneNumberActivity.this,Constants.LOGIN_UID,0));
                maps.put("password", Des3.getEncodePWD(inputPwd));
                MyHttp.requestByFailCallback(Constants.BASE_URL + Constants.POST_VERIFY_PWD_URL, maps, MyHttp.POST, PhoneNumberActivity.this, false, null, new SimplePaserJsonCallback() {
                    @Override
                    public void onVerifyIsNull() {
                        //跳转到修改手机号界面
                        Intent intent = new Intent(PhoneNumberActivity.this, ChangePhoneActivity.class);
                        startActivityForResult(intent, REQUEST_CODE);
                        DialogFactory.dismissLoadingDialog(loadingDialog);
                    }

                    @Override
                    public void onVerifySuccess(String dataRaw, boolean isCache) {

                    }

                    @Override
                    public void onErrorCodeError(BaseEntity.MyError mError) {
                        super.onErrorCodeError(mError);
                        DialogFactory.dismissLoadingDialog(loadingDialog);
                        DialogFactory.warningDialog(PhoneNumberActivity.this, mError.msg);
                    }
                }, new HttpFailCallback() {
                    @Override
                    public void onServiceErrorCallback() {
                        DialogFactory.dismissLoadingDialog(loadingDialog);
                    }
                });

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent arg2) {
        super.onActivityResult(requestCode, resultCode, arg2);
        if (resultCode == RESULT_OK){
            if (requestCode == REQUEST_CODE) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }
}
